﻿using Questionary.Logic;
using Questionary.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Questionary.Views
{
	public partial class Questionary : System.Web.UI.Page
	{
        protected string QuestionsData = "";

        protected List<QuestionItem> Questions = new List<QuestionItem>();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				LoadData();
			}
		}

		private void LoadData()
		{
			Questions = FileHelper.GetObject<List<QuestionItem>>(Request.MapPath(FilePaths.QuestionFilePath));
			if (Questions == null) Questions = new List<QuestionItem>();

            StringBuilder dataBuilder = new StringBuilder();
            foreach (QuestionItem qItem in Questions)
            {
                dataBuilder.Append(string.Format("[{0},{1},{2}],", qItem.ID, qItem.QType, qItem.Selections.Count));
            }

            QuestionsData = dataBuilder.ToString().Trim(',');
		}

		protected void btnSaveItem_Click(object sender, EventArgs e)
		{
			ActiveItem activeItem = new ActiveItem();
            activeItem.SchoolName = Request.Form["cbbSchool"];
			activeItem.Name = txtName.Text.Trim();
			activeItem.IP = Request.UserHostAddress;
			activeItem.CreateTime = DateTime.Now;
			Questions = FileHelper.GetObject<List<QuestionItem>>(Request.MapPath(FilePaths.QuestionFilePath));

			int counter = 1;
			foreach (var qItem in Questions)
			{
				if (qItem.QType == 2)//文字
				{
                    activeItem.Answers.Add(new AnswerItem() { QuestionID = qItem.ID, ID = counter++, Content = Request.Form["txt_"+qItem.ID]});
                }
				else if(qItem.QType == 1)//多选
				{
                    var answer = new AnswerItem() { QuestionID = qItem.ID, ID = counter++ };
                    foreach (var sItem in qItem.Selections)
                    {
                        if(!string.IsNullOrEmpty(Request.Form["cbb_" + qItem.ID + "_" + sItem.ID]))
                        {
                            answer.Selections.Add(sItem.ID);
                        }
                    }
                    activeItem.Answers.Add(answer);
                }
				else
				{
                    var answer = new AnswerItem() { QuestionID = qItem.ID, ID = counter++};

                    int selectValue = 0;
                    if(int.TryParse(Request.Form["rbG" + qItem.ID], out selectValue))
                    {
                        answer.Selections.Add(selectValue);
                        activeItem.Answers.Add(answer);
                    }
                }
			}

            if (!activeItem.IsValid())
            {
                Response.Write("提交出错。");
                Response.End();
                return;
            }
            else
            {
                string fileName = "Active_" + DateTime.Now.ToString("yy_MM_dd_") + DateTime.Now.Millisecond + ".xml";
                FileHelper.WriteObject<ActiveItem>(activeItem, Request.MapPath(FilePaths.ActivityFolder + fileName));
                Response.Redirect("welcome.aspx");
            }

            
        }
	}
}